package smarthome

import "fmt"

// Device 设备接口
type Device interface {
	On()
	Off()
	GetName() string
	GetStatus() string
}

// Light 具体接收者 - 灯
type Light struct {
	Name   string
	IsOn   bool
	Bright int // 亮度 0-100
}

func NewLight(name string) *Light {
	return &Light{
		Name:   name,
		Bright: 50,
	}
}

func (l *Light) On() {
	l.IsOn = true
	fmt.Printf("%s灯已打开\n", l.Name)
}

func (l *Light) Off() {
	l.IsOn = false
	fmt.Printf("%s灯已关闭\n", l.Name)
}

func (l *Light) GetBright() int {
	return l.Bright
}

func (l *Light) SetBright(bright int) {
	if bright < 0 {
		bright = 0
	}
	if bright > 100 {
		bright = 100
	}
	l.Bright = bright
	fmt.Printf("%s灯已设置亮度为%d\n", l.Name, l.Bright)
}

func (l *Light) GetName() string {
	return l.Name
}

func (l *Light) GetStatus() string {
	if l.IsOn {
		return fmt.Sprintf("开 (亮度%d%%)", l.Bright)
	}
	return "关"
}

// AirConditioner 具体接收者 - 空调
type AirConditioner struct {
	Name        string
	IsOn        bool
	Temperature int    // 温度
	Mode        string // 模式 cool/head/fan
	FanSpeed    string // 风速 1-3
}

func NewAirConditioner(name string) *AirConditioner {
	return &AirConditioner{
		Name: name,
		Temperature: 25,
		Mode: "cool",
		FanSpeed: "1",
	}
}

func (a *AirConditioner) On() {
	a.IsOn = true
	fmt.Printf("%s空调已打开\n", a.Name)
}

func (a *AirConditioner) Off() {
	a.IsOn = false
	fmt.Printf("%s空调已关闭\n", a.Name)
}

func (a *AirConditioner) GetTemperature() int {
	return a.Temperature
}

func (a *AirConditioner) SetTemperature(temp int) {
	a.Temperature = temp
	fmt.Printf("%s空调温度设置为%d℃\n", a.Name, temp)
}

func (a *AirConditioner) GetMode() string {
	return a.Mode
}

func (a *AirConditioner) SetMode(mode string) {
	a.Mode = mode
	fmt.Printf("%s空调模式设置为%s\n", a.Name, a.Mode)
}

func (a *AirConditioner) GetFanSpeed() string {
	return a.FanSpeed
}

func (a *AirConditioner) SetFanSpeed(speed string) {
	a.FanSpeed = speed
	fmt.Printf("%s空调风速设置为%s\n", a.Name, a.FanSpeed)
}

func (a *AirConditioner) GetName() string {
	return a.Name
}

func (a *AirConditioner) GetStatus() string {
	if a.IsOn {
		return fmt.Sprintf("开 (温度%d℃, 模式%s, 风速%s档)", a.Temperature, a.Mode, a.FanSpeed)
	}
	return "关"
}
